<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Irrlicht 3D Engine: Tutorial 7: Collision</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  <td id="projectlogo"><img alt="Logo" src="irrlichtlogo.png"/></td>
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">Irrlicht 3D Engine
   
   </div>
   
  </td>
  
  
  
   
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="dynsections.js"></script>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('example007.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Tutorial 7: Collision </div>  </div>
</div>
<div class="contents">
<div class="textblock"><div class="image">
<img src="007shot.jpg" alt="007shot.jpg"/>
</div>
 <p>We will describe 2 methods: Automatic collision detection for moving through 3d worlds with stair climbing and sliding, and manual scene node and triangle picking using a ray. In this case, we will use a ray coming out from the camera, but you can use any ray.</p>
<p>To start, we take the program from tutorial 2, which loads and displays a quake 3 level. We will use the level to walk in it and to pick triangles from. In addition we'll place 3 animated models into it for triangle picking. The following code starts up the engine and loads the level, as per tutorial 2. </p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>&gt;</span>
<span class="preprocessor">#include &quot;<a class="code" href="driver_choice_8h.html">driverChoice.h</a>&quot;</span>

<span class="keyword">using namespace </span>irr;

<span class="preprocessor">#ifdef _MSC_VER</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, &quot;Irrlicht.lib&quot;)</span>
<span class="preprocessor"></span><span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="keyword">enum</span>
{
    <span class="comment">// I use this ISceneNode ID to indicate a scene node that is</span>
    <span class="comment">// not pickable by getSceneNodeAndCollisionPointFromRay()</span>
    ID_IsNotPickable = 0,

    <span class="comment">// I use this flag in ISceneNode IDs to indicate that the</span>
    <span class="comment">// scene node can be picked by ray selection.</span>
    IDFlag_IsPickable = 1 &lt;&lt; 0,

    <span class="comment">// I use this flag in ISceneNode IDs to indicate that the</span>
    <span class="comment">// scene node can be highlighted.  In this example, the</span>
    <span class="comment">// homonids can be highlighted, but the level mesh can&#39;t.</span>
    IDFlag_IsHighlightable = 1 &lt;&lt; 1
};

<span class="keywordtype">int</span> main()
{
    <span class="comment">// ask user for driver</span>
    <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">video::E_DRIVER_TYPE</a> driverType=driverChoiceConsole();
    <span class="keywordflow">if</span> (driverType==<a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0ae685cada50f8c100403134d932d0414c" title="No driver, just for counting the elements.">video::EDT_COUNT</a>)
        <span class="keywordflow">return</span> 1;

    <span class="comment">// create device</span>

    <a class="code" href="classirr_1_1_irrlicht_device.html" title="The Irrlicht device. You can create it with createDevice() or createDeviceEx().">IrrlichtDevice</a> *device =
        <a class="code" href="namespaceirr.html#abaf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the engine.">createDevice</a>(driverType, <a class="code" href="classirr_1_1core_1_1dimension2d.html">core::dimension2d&lt;u32&gt;</a>(640, 480), 16, <span class="keyword">false</span>);

    <span class="keywordflow">if</span> (device == 0)
        <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span>

    <a class="code" href="classirr_1_1video_1_1_i_video_driver.html" title="Interface to driver which is able to perform 2d and 3d graphics functions.">video::IVideoDriver</a>* driver = device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#ada90707ba5c645d47e000e4e0f87c4c4" title="Provides access to the video driver for drawing 3d and 2d geometry.">getVideoDriver</a>();
    <a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html" title="The Scene Manager manages scene nodes, mesh recources, cameras and all the other stuff.">scene::ISceneManager</a>* smgr = device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#a891b503ff4d5041296d88f23f97d7b3d" title="Provides access to the scene manager.">getSceneManager</a>();

    device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#a3d8d2dee2f57aa7e6c0d14592de3e6ed" title="Provides access to the virtual file system.">getFileSystem</a>()-&gt;<a class="code" href="classirr_1_1io_1_1_i_file_system.html#afe6641c7f88a8fea0205c113b8379730" title="Adds an archive to the file system.">addFileArchive</a>(<span class="stringliteral">&quot;../../media/map-20kdm2.pk3&quot;</span>);

    <a class="code" href="classirr_1_1scene_1_1_i_animated_mesh.html" title="Interface for an animated mesh.">scene::IAnimatedMesh</a>* q3levelmesh = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a63894c3f3d46cfc385116f1705935e03" title="Get pointer to an animateable mesh. Loads the file if not loaded already.">getMesh</a>(<span class="stringliteral">&quot;20kdm2.bsp&quot;</span>);
    <a class="code" href="classirr_1_1scene_1_1_i_mesh_scene_node.html" title="A scene node displaying a static mesh.">scene::IMeshSceneNode</a>* q3node = 0;

    <span class="comment">// The Quake mesh is pickable, but doesn&#39;t get highlighted.</span>
    <span class="keywordflow">if</span> (q3levelmesh)
        q3node = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a503339385ca2b33d7e8035a61c4eca84" title="Adds a scene node for rendering using a octree to the scene graph.">addOctreeSceneNode</a>(q3levelmesh-&gt;<a class="code" href="classirr_1_1scene_1_1_i_animated_mesh.html#adccb39fee83bed36a464cf7b96f3a0ca" title="Returns the IMesh interface for a frame.">getMesh</a>(0), 0, IDFlag_IsPickable);
</pre></div><p>So far so good, we've loaded the quake 3 level like in tutorial 2. Now, here comes something different: We create a triangle selector. A triangle selector is a class which can fetch the triangles from scene nodes for doing different things with them, for example collision detection. There are different triangle selectors, and all can be created with the ISceneManager. In this example, we create an OctreeTriangleSelector, which optimizes the triangle output a little bit by reducing it like an octree. This is very useful for huge meshes like quake 3 levels. After we created the triangle selector, we attach it to the q3node. This is not necessary, but in this way, we do not need to care for the selector, for example dropping it after we do not need it anymore. </p>
<div class="fragment"><pre class="fragment">    scene::ITriangleSelector* selector = 0;

    <span class="keywordflow">if</span> (q3node)
    {
        q3node-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_node.html#a2166eb0a92cc0e46c49266f41a68ed50" title="Sets the position of the node relative to its parent.">setPosition</a>(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-1350,-130,-1400));

        selector = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a4ed7d3b34f4d0c70395b6d464fe32b96" title="Creates a Triangle Selector, optimized by an octree.">createOctreeTriangleSelector</a>(
                q3node-&gt;<a class="code" href="classirr_1_1scene_1_1_i_mesh_scene_node.html#afe540de69bc3a058919cd5ce465be634" title="Get the currently defined mesh for display.">getMesh</a>(), q3node, 128);
        q3node-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_node.html#a87fb52ec54bf3ae117340d8defd1255f" title="Sets the triangle selector of the scene node.">setTriangleSelector</a>(selector);
        <span class="comment">// We&#39;re not done with this selector yet, so don&#39;t drop it.</span>
    }
</pre></div><p>We add a first person shooter camera to the scene so that we can see and move in the quake 3 level like in tutorial 2. But this, time, we add a special animator to the camera: A Collision Response animator. This animator modifies the scene node to which it is attached to in order to prevent it moving through walls, and to add gravity to it. The only thing we have to tell the animator is how the world looks like, how big the scene node is, how much gravity to apply and so on. After the collision response animator is attached to the camera, we do not have to do anything more for collision detection, anything is done automatically. The rest of the collision detection code below is for picking. And please note another cool feature: The collision response animator can be attached also to all other scene nodes, not only to cameras. And it can be mixed with other scene node animators. In this way, collision detection and response in the Irrlicht engine is really easy.</p>
<p>Now we'll take a closer look on the parameters of createCollisionResponseAnimator(). The first parameter is the TriangleSelector, which specifies how the world, against collision detection is done looks like. The second parameter is the scene node, which is the object, which is affected by collision detection, in our case it is the camera. The third defines how big the object is, it is the radius of an ellipsoid. Try it out and change the radius to smaller values, the camera will be able to move closer to walls after this. The next parameter is the direction and speed of gravity. We'll set it to (0, -10, 0), which approximates to realistic gravity, assuming that our units are metres. You could set it to (0,0,0) to disable gravity. And the last value is just a translation: Without this, the ellipsoid with which collision detection is done would be around the camera, and the camera would be in the middle of the ellipsoid. But as human beings, we are used to have our eyes on top of the body, with which we collide with our world, not in the middle of it. So we place the scene node 50 units over the center of the ellipsoid with this parameter. And that's it, collision detection works now. </p>
<div class="fragment"><pre class="fragment">    <span class="comment">// Set a jump speed of 3 units per second, which gives a fairly realistic jump</span>
    <span class="comment">// when used with the gravity of (0, -10, 0) in the collision response animator.</span>
    scene::ICameraSceneNode* camera =
        smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#ac312cbc85161678d00192880f2cdddbb" title="Adds a camera scene node with an animator which provides mouse and keyboard control appropriate for f...">addCameraSceneNodeFPS</a>(0, 100.0f, .3f, ID_IsNotPickable, 0, 0, <span class="keyword">true</span>, 3.f);
    camera-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(50,50,-60));
    camera-&gt;setTarget(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-70,30,-60));

    <span class="keywordflow">if</span> (selector)
    {
        scene::ISceneNodeAnimator* anim = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a67b266cc40ebd66b5d21c26a78f002be" title="Creates a special scene node animator for doing automatic collision detection and response...">createCollisionResponseAnimator</a>(
            selector, camera, <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(30,50,30),
            <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,-10,0), <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,30,0));
        selector-&gt;drop(); <span class="comment">// As soon as we&#39;re done with the selector, drop it.</span>
        camera-&gt;addAnimator(anim);
        anim-&gt;drop();  <span class="comment">// And likewise, drop the animator when we&#39;re done referring to it.</span>
    }

    <span class="comment">// Now I create three animated characters which we can pick, a dynamic light for</span>
    <span class="comment">// lighting them, and a billboard for drawing where we found an intersection.</span>

    <span class="comment">// First, let&#39;s get rid of the mouse cursor.  We&#39;ll use a billboard to show</span>
    <span class="comment">// what we&#39;re looking at.</span>
    device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#a500a3b7bf69487ff7e2075dd0b0db529" title="Provides access to the cursor control.">getCursorControl</a>()-&gt;setVisible(<span class="keyword">false</span>);

    <span class="comment">// Add the billboard.</span>
    scene::IBillboardSceneNode * bill = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a00266a58b97e827acd9e105806a99c3a" title="Adds a billboard scene node to the scene graph.">addBillboardSceneNode</a>();
    bill-&gt;setMaterialType(<a class="code" href="namespaceirr_1_1video.html#ac8e9b6c66f7cebabd1a6d30cbc5430f1a1b5a814c4466aca2943ff056003a50d1" title="A transparent material.">video::EMT_TRANSPARENT_ADD_COLOR</a> );
    bill-&gt;setMaterialTexture(0, driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#af4055165190e4adf221c6dc6f2434ea0" title="Get access to a named texture.">getTexture</a>(<span class="stringliteral">&quot;../../media/particle.bmp&quot;</span>));
    bill-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
    bill-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3a493bb44efafebb48adab96e31eb029e5" title="Is the ZBuffer enabled? Default: true.">video::EMF_ZBUFFER</a>, <span class="keyword">false</span>);
    bill-&gt;setSize(core::dimension2d&lt;f32&gt;(20.0f, 20.0f));
    bill-&gt;setID(ID_IsNotPickable); <span class="comment">// This ensures that we don&#39;t accidentally ray-pick it</span>
</pre></div><p> Add 3 animated hominids, which we can pick using a ray-triangle intersection. They all animate quite slowly, to make it easier to see that accurate triangle selection is being performed. </p>
<div class="fragment"><pre class="fragment">    scene::IAnimatedMeshSceneNode* node = 0;

    video::SMaterial material;

    <span class="comment">// Add an MD2 node, which uses vertex-based animation.</span>
    node = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a8e2e0cd3a27e85b4116855dd2f3365b8" title="Adds a scene node for rendering an animated mesh model.">addAnimatedMeshSceneNode</a>(smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a63894c3f3d46cfc385116f1705935e03" title="Get pointer to an animateable mesh. Loads the file if not loaded already.">getMesh</a>(<span class="stringliteral">&quot;../../media/faerie.md2&quot;</span>),
                        0, IDFlag_IsPickable | IDFlag_IsHighlightable);
    node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-90,-15,-140)); <span class="comment">// Put its feet on the floor.</span>
    node-&gt;setScale(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(1.6f)); <span class="comment">// Make it appear realistically scaled</span>
    node-&gt;setMD2Animation(<a class="code" href="namespaceirr_1_1scene.html#a08d4a84966e1d2886d0d57e4acbb4f19aac114a5c69f38d4f943145dd98929d95">scene::EMAT_POINT</a>);
    node-&gt;setAnimationSpeed(20.f);
    material.setTexture(0, driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#af4055165190e4adf221c6dc6f2434ea0" title="Get access to a named texture.">getTexture</a>(<span class="stringliteral">&quot;../../media/faerie2.bmp&quot;</span>));
    material.Lighting = <span class="keyword">true</span>;
    material.NormalizeNormals = <span class="keyword">true</span>;
    node-&gt;getMaterial(0) = material;

    <span class="comment">// Now create a triangle selector for it.  The selector will know that it</span>
    <span class="comment">// is associated with an animated node, and will update itself as necessary.</span>
    selector = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a266625379b1558e9be1dc062ea4e71f7" title="Creates a simple ITriangleSelector, based on a mesh.">createTriangleSelector</a>(node);
    node-&gt;setTriangleSelector(selector);
    selector-&gt;drop(); <span class="comment">// We&#39;re done with this selector, so drop it now.</span>

    <span class="comment">// And this B3D file uses skinned skeletal animation.</span>
    node = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a8e2e0cd3a27e85b4116855dd2f3365b8" title="Adds a scene node for rendering an animated mesh model.">addAnimatedMeshSceneNode</a>(smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a63894c3f3d46cfc385116f1705935e03" title="Get pointer to an animateable mesh. Loads the file if not loaded already.">getMesh</a>(<span class="stringliteral">&quot;../../media/ninja.b3d&quot;</span>),
                        0, IDFlag_IsPickable | IDFlag_IsHighlightable);
    node-&gt;setScale(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(10));
    node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-75,-66,-80));
    node-&gt;setRotation(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,90,0));
    node-&gt;setAnimationSpeed(8.f);
    node-&gt;getMaterial(0).NormalizeNormals = <span class="keyword">true</span>;
    node-&gt;getMaterial(0).Lighting = <span class="keyword">true</span>;
    <span class="comment">// Just do the same as we did above.</span>
    selector = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a266625379b1558e9be1dc062ea4e71f7" title="Creates a simple ITriangleSelector, based on a mesh.">createTriangleSelector</a>(node);
    node-&gt;setTriangleSelector(selector);
    selector-&gt;drop();

    <span class="comment">// This X files uses skeletal animation, but without skinning.</span>
    node = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a8e2e0cd3a27e85b4116855dd2f3365b8" title="Adds a scene node for rendering an animated mesh model.">addAnimatedMeshSceneNode</a>(smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a63894c3f3d46cfc385116f1705935e03" title="Get pointer to an animateable mesh. Loads the file if not loaded already.">getMesh</a>(<span class="stringliteral">&quot;../../media/dwarf.x&quot;</span>),
                        0, IDFlag_IsPickable | IDFlag_IsHighlightable);
    node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-70,-66,-30)); <span class="comment">// Put its feet on the floor.</span>
    node-&gt;setRotation(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,-90,0)); <span class="comment">// And turn it towards the camera.</span>
    node-&gt;setAnimationSpeed(20.f);
    node-&gt;getMaterial(0).Lighting = <span class="keyword">true</span>;
    selector = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a266625379b1558e9be1dc062ea4e71f7" title="Creates a simple ITriangleSelector, based on a mesh.">createTriangleSelector</a>(node);
    node-&gt;setTriangleSelector(selector);
    selector-&gt;drop();


    <span class="comment">// And this mdl file uses skinned skeletal animation.</span>
    node = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a8e2e0cd3a27e85b4116855dd2f3365b8" title="Adds a scene node for rendering an animated mesh model.">addAnimatedMeshSceneNode</a>(smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a63894c3f3d46cfc385116f1705935e03" title="Get pointer to an animateable mesh. Loads the file if not loaded already.">getMesh</a>(<span class="stringliteral">&quot;../../media/yodan.mdl&quot;</span>),
                        0, IDFlag_IsPickable | IDFlag_IsHighlightable);
    node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-90,-25,20));
    node-&gt;setScale(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0.8f));
    node-&gt;getMaterial(0).Lighting = <span class="keyword">true</span>;
    node-&gt;setAnimationSpeed(20.f);

    <span class="comment">// Just do the same as we did above.</span>
    selector = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a266625379b1558e9be1dc062ea4e71f7" title="Creates a simple ITriangleSelector, based on a mesh.">createTriangleSelector</a>(node);
    node-&gt;setTriangleSelector(selector);
    selector-&gt;drop();

    material.setTexture(0, 0);
    material.Lighting = <span class="keyword">false</span>;

    <span class="comment">// Add a light, so that the unselected nodes aren&#39;t completely dark.</span>
    scene::ILightSceneNode * light = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a2e6442f8c95a544c355bd137ccdb7095" title="Adds a dynamic light scene node to the scene graph.">addLightSceneNode</a>(0, <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-60,100,400),
        video::SColorf(1.0f,1.0f,1.0f,1.0f), 600.0f);
    light-&gt;setID(ID_IsNotPickable); <span class="comment">// Make it an invalid target for selection.</span>

    <span class="comment">// Remember which scene node is highlighted</span>
    scene::ISceneNode* highlightedSceneNode = 0;
    scene::ISceneCollisionManager* collMan = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a433b65bfc974d069a1dc2fc296b23d9b" title="Get pointer to the scene collision manager.">getSceneCollisionManager</a>();
    <span class="keywordtype">int</span> lastFPS = -1;

    <span class="comment">// draw the selection triangle only as wireframe</span>
    material.Wireframe=<span class="keyword">true</span>;

    <span class="keywordflow">while</span>(device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#a0489f8151dc43f6f41503ffb5a160b35" title="Runs the device.">run</a>())
    <span class="keywordflow">if</span> (device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#abd3c88336b739da2694883d5ffd25a70" title="Returns if the window is active.">isWindowActive</a>())
    {
        driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#a015b8f2f18c260a00a858181be1e9945" title="Applications must call this method before performing any rendering.">beginScene</a>(<span class="keyword">true</span>, <span class="keyword">true</span>, 0);
        smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#a04240262904667c821bd9de5e5fd9b02" title="Draws all the scene nodes.">drawAll</a>();

        <span class="comment">// Unlight any currently highlighted scene node</span>
        <span class="keywordflow">if</span> (highlightedSceneNode)
        {
            highlightedSceneNode-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">true</span>);
            highlightedSceneNode = 0;
        }

        <span class="comment">// All intersections in this example are done with a ray cast out from the camera to</span>
        <span class="comment">// a distance of 1000.  You can easily modify this to check (e.g.) a bullet</span>
        <span class="comment">// trajectory or a sword&#39;s position, or create a ray from a mouse click position using</span>
        <span class="comment">// ISceneCollisionManager::getRayFromScreenCoordinates()</span>
        core::line3d&lt;f32&gt; ray;
        ray.start = camera-&gt;getPosition();
        ray.end = ray.start + (camera-&gt;getTarget() - ray.start).normalize() * 1000.0f;

        <span class="comment">// Tracks the current intersection point with the level or a mesh</span>
        <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a> intersection;
        <span class="comment">// Used to show with triangle has been hit</span>
        <a class="code" href="namespaceirr_1_1core.html#a1112835405bbec5dadf031dc7934e7d0" title="Typedef for a f32 3d triangle.">core::triangle3df</a> hitTriangle;

        <span class="comment">// This call is all you need to perform ray/triangle collision on every scene node</span>
        <span class="comment">// that has a triangle selector, including the Quake level mesh.  It finds the nearest</span>
        <span class="comment">// collision point/triangle, and returns the scene node containing that point.</span>
        <span class="comment">// Irrlicht provides other types of selection, including ray/triangle selector,</span>
        <span class="comment">// ray/box and ellipse/triangle selector, plus associated helpers.</span>
        <span class="comment">// See the methods of ISceneCollisionManager</span>
        scene::ISceneNode * selectedSceneNode =
            collMan-&gt;getSceneNodeAndCollisionPointFromRay(
                    ray,
                    intersection, <span class="comment">// This will be the position of the collision</span>
                    hitTriangle, <span class="comment">// This will be the triangle hit in the collision</span>
                    IDFlag_IsPickable, <span class="comment">// This ensures that only nodes that we have</span>
                            <span class="comment">// set up to be pickable are considered</span>
                    0); <span class="comment">// Check the entire scene (this is actually the implicit default)</span>

        <span class="comment">// If the ray hit anything, move the billboard to the collision position</span>
        <span class="comment">// and draw the triangle that was hit.</span>
        <span class="keywordflow">if</span>(selectedSceneNode)
        {
            bill-&gt;setPosition(intersection);

            <span class="comment">// We need to reset the transform before doing our own rendering.</span>
            driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#aaf6e88bedf7b91666a2bd34f46e092fc" title="Sets transformation matrices.">setTransform</a>(<a class="code" href="namespaceirr_1_1video.html#a15b57657a320243be03ae6f66fcff43da843cf42adb3fa9caf61c9e228cf14e85" title="World transformation.">video::ETS_WORLD</a>, <a class="code" href="namespaceirr_1_1core.html#a73fa92e638c5ca97efd72da307cc9b65" title="Typedef for f32 matrix.">core::matrix4</a>());
            driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#a8c9e31b41b7e6fd26cf65ce538ebab05" title="Sets a material.">setMaterial</a>(material);
            driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#ac5eb03a333a43d17278dad31be19efca" title="Draws a 3d triangle.">draw3DTriangle</a>(hitTriangle, video::SColor(0,255,0,0));

            <span class="comment">// We can check the flags for the scene node that was hit to see if it should be</span>
            <span class="comment">// highlighted. The animated nodes can be highlighted, but not the Quake level mesh</span>
            <span class="keywordflow">if</span>((selectedSceneNode-&gt;getID() &amp; IDFlag_IsHighlightable) == IDFlag_IsHighlightable)
            {
                highlightedSceneNode = selectedSceneNode;

                <span class="comment">// Highlighting in this case means turning lighting OFF for this node,</span>
                <span class="comment">// which means that it will be drawn with full brightness.</span>
                highlightedSceneNode-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
            }
        }

        <span class="comment">// We&#39;re all done drawing, so end the scene.</span>
        driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#a75f61a93c5fc9fdf161c044d27bc994e" title="Presents the rendered image to the screen.">endScene</a>();

        <span class="keywordtype">int</span> fps = driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#a5b71428402c0b6a3b18b8f2fa408af13" title="Returns current frames per second value.">getFPS</a>();

        <span class="keywordflow">if</span> (lastFPS != fps)
        {
            <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">core::stringw</a> str = L<span class="stringliteral">&quot;Collision detection example - Irrlicht Engine [&quot;</span>;
            str += driver-&gt;<a class="code" href="classirr_1_1video_1_1_i_video_driver.html#a87ca51832295b2dceaa1e258daf863f1" title="Gets name of this video driver.">getName</a>();
            str += <span class="stringliteral">&quot;] FPS:&quot;</span>;
            str += fps;

            device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#a3d7c98d520bf18ce1973c6f1439a7c0f" title="Sets the caption of the window.">setWindowCaption</a>(str.c_str());
            lastFPS = fps;
        }
    }

    device-&gt;<a class="code" href="classirr_1_1_i_reference_counted.html#afb169a857e0d2cdb96b8821cb9bff17a" title="Drops the object. Decrements the reference counter by one.">drop</a>();

    <span class="keywordflow">return</span> 0;
}
</pre></div> </div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>


    <li class="footer">
<a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht 
Engine</a> Documentation &copy; 2003-2012 by Nikolaus Gebhardt. Generated on Tue Jan 19 2016 16:08:47 for Irrlicht 3D Engine by
<a href="http://www.doxygen.org/index.html" target="_blank">Doxygen</a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
